using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DataLite
{
    public class FileHelper
    {
        public static string GetAssemblyFolder()
        {
            string oRoot = System.Reflection.Assembly.GetExecutingAssembly().Location;
            System.IO.FileInfo fileInfo = new FileInfo(oRoot);

            return fileInfo.Directory.FullName;
        }

        /// <summary>
        /// Lee un fichero y lo devuelve en texto
        /// </summary>
        /// <param name="path">ruta</param>
        /// <param name="file">archivo</param>
        /// <returns></returns>
        public static string GetFile(string path, string file)
        {
            string fullName = Path.Combine(path, file);
            return GetFile(fullName);
        }

        public static string GetFile(string fileFullName)
        {            
            string output = null;
            if (File.Exists(fileFullName))
            {
                using (StreamReader streamReader = new StreamReader(GetStream(fileFullName)))
                {
                    output = streamReader.ReadToEnd();
                }
            }
            return output;
        }

        static Stream GetStream(string fileFullName)
        {
            return File.Open(fileFullName, FileMode.Open, FileAccess.Read);
        }

        /// <summary>
        /// Copia un fichero
        /// </summary>
        /// <param name="fileFullName"></param>
        /// <param name="outPutPath"></param>
        public static void CopyFile(string fileFullName, string outPutPath)
        {
            using (FileStream fileStream = new FileStream(outPutPath, FileMode.Create))
            {
                using (Stream stream = GetStream(fileFullName))
                {
                    while (true)
                    {
                        int intValue = stream.ReadByte();
                        if (intValue >= 0)
                        {
                            byte byteValue = (byte)intValue;
                            fileStream.WriteByte(byteValue);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
        }

        public static void SaveFile(string aText, string aOutPutPath)
        {
            if (File.Exists(aOutPutPath)==false)
            {
                using (StreamWriter streamWriter = new StreamWriter(aOutPutPath))
                {
                    streamWriter.Write(aText);
                }
            }            
        }

        public static void SaveFile(string aText, string aOutPutPath, bool deleteIfExists)
        {
            if (File.Exists(aOutPutPath))
            {
                File.Delete(aOutPutPath);
            }

            using (StreamWriter streamWriter = new StreamWriter(aOutPutPath))
            {
                streamWriter.Write(aText);
            }
            
        }

        #region FolderManager

        /// <summary>
        /// Creates the specified sub-directory, if it doesn't exist.
        /// </summary>
        /// <param name="name">The name of the sub-directory to be created.</param>
        public static void CreateSubDirectory(string name)
        {
            if (Directory.Exists(name) == false)
            {
                Directory.CreateDirectory(name);
            }
        }

        /// <summary>
        /// Creates the specified sub-directory, if it doesn't exist.
        /// </summary>
        /// <param name="name">The name of the sub-directory to be created.</param>
        /// <param name="deleteIfExists">Indicates if the directory should be deleted if it exists.</param>
        public static void CreateSubDirectory(string name, bool deleteIfExists)
        {
            if (Directory.Exists(name))
            {
                Directory.Delete(name, true);
            }

            Directory.CreateDirectory(name);
        }

        #endregion

    }
}
